﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel; 


namespace Zoomn
{
    public partial class Form1 : Form
    {
        public List<string> a = new List<string>(); // 05
        public List<string> b = new List<string>(); // 07
        public List<string> c = new List<string>();  // 18
        string filename = @"C:\g.gdf";
        string filename3 = @"C:\Users\Zadik\Desktop\new3.txt";
        string zoom1 = @"C:\Users\Zadik\Desktop\new4.txt";
        public int totaal;

        public Form1()
        {
            InitializeComponent();
            ParseFile();
            comboBox1.Items.Add("05");
            comboBox1.Items.Add("07");
            comboBox1.Items.Add("18");
            comboBox1.Items.Add("05-18");
            comboBox1.Items.Add("05-17-18");
            schrijfweg();


            //Land Use and Land Cover Display Types 
            comboBox2.Items.Add("Beach/Dune/Sand Area"); // 7S
            comboBox2.Items.Add("Park/Garden"); //  PT
            comboBox2.Items.Add("Forest"); // 5F
            comboBox2.Items.Add("Company Ground"); //8G  
            comboBox2.Items.Add("Functional Road Class "); //FC  
            

        }
 

      public  void ParseFile()
        {
            StreamReader sr = File.OpenText(filename);
            long totalbytes = 0;
            try
            {
                sr = File.OpenText(filename);
                totalbytes = sr.BaseStream.Length;
            }
            catch (Exception e)
            {
                Console.WriteLine("No GDF file found.");
                Environment.Exit(0);
            }

            while (!sr.EndOfStream)
            {
                String str = sr.ReadLine();
                String id = str.Substring(0, 2);

                    switch(id)
                    {
                        case ("05"):
                            string var = str.Substring(2, 6);
                            string ATT_TYPE = str.Substring(2, 2);
                            string FIELD_SIZE = str.Substring(4, 2);
                            string DATA_TYPE = str.Substring(6, 2).Trim();
                            string ATT_DESC = str.Substring(30, 49).Trim();
                            a.Add(id + "|" + ATT_TYPE + "|" + FIELD_SIZE + "|" + DATA_TYPE + "|" + ATT_DESC);
                            break;
                        case ("07"):
                            string FEAT_CODE = str.Substring(2, 4).Trim();
                                string FEAT_NAME = str.Substring(6, 40).Trim();
                                string var1 = str.Substring(0, 6).Trim();
                                b.Add(id + "|" + FEAT_CODE + "|" + FEAT_NAME);
                            break;
                        case ("18"):
                            string var2 = str.Substring(0, 14).Trim();
                            string ATT_TYPE2 = str.Substring(2, 2);
                            string ATT_VAL2 = str.Substring(4, 10).Trim();
                            string VAL_DESC2 = str.Substring(17, 30).Trim();
                          //  Console.WriteLine(VAL_DESC2);
                            c.Add(id + "|" + ATT_TYPE2 + "|" + ATT_VAL2 + "|" + VAL_DESC2);
                            break;
                    }
            }
            
        }

        public void schrijfweg()
        {

            using (StreamWriter sw = new StreamWriter(filename3))
            {
                sw.WriteLine("05 - REC_DESCR ATT_TYPE  FIELD_SIZE  DATA_TYPE  ATT_DESC");
                foreach (string l1 in a)
                {
                    sw.WriteLine(l1);
                }

                sw.WriteLine("07 - REC_DESCR  FEAT_CODE  FEAT_NAME");
                foreach (string l2 in b)
                {
                    sw.WriteLine(l2);
                }

                sw.WriteLine("18 - REC_DESCR  ATT_TYPE  ATT_VAL  ATT_DESC");
                foreach (string l3 in c)
                {
                    sw.WriteLine(l3);
                }
            }
        }

        public void maaka()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("REC_DESCR");
            dt.Columns.Add("ATT_TYPE");
            dt.Columns.Add("FIELD_SIZE");
            dt.Columns.Add("DATA_TYPE");
            dt.Columns.Add("ATT_DESC");

            using (StreamWriter sw = new StreamWriter(filename3))
            {
                foreach (string l1 in a)
                {
                    string[] part = l1.Split('|');
                    dt.Rows.Add(part[0], part[1], part[2], part[3], part[4]);
                    totaal++;
                }
            }
            dataGridView1.DataSource = dt;
        }

        public void maakb()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("REC_DESCR");
            dt.Columns.Add("FEAT_CODE");
            dt.Columns.Add("FEAT_NAME");

            using (StreamWriter sw = new StreamWriter(filename3))
            {
                foreach (string l1 in b)
                {
                   string[] part = l1.Split('|');
                   dt.Rows.Add(part[0], part[1], part[2]);
                   totaal++;
                   // Console.WriteLine(l1);
                }
            }
            dataGridView1.DataSource = dt;
        }

        public void maakc()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("REC_DESCR");
            dt.Columns.Add("ATT_TYPE");
            dt.Columns.Add("ATT_VAL");
            dt.Columns.Add("ATT_DESC");

            using (StreamWriter sw = new StreamWriter(filename3))
            {
                foreach (string l1 in c)
                {
                    string[] part = l1.Split('|');
                    dt.Rows.Add(part[0], part[1], part[2], part[3]);
                    totaal++;
                }
            }
            dataGridView1.DataSource = dt;
 
        }


        public void maakd()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("1");
            dt.Columns.Add("2");
            dt.Columns.Add("3");
            dt.Columns.Add("4");
            dt.Columns.Add("5");

            dt.Rows.Add("REC_DESCR", "ATT_TYPE", "FIELD_SIZE", "DATA_TYPE", "ATT_DESC");

            using (StreamWriter sw = new StreamWriter(filename3))
            {
                foreach (string l1 in a)
                {
                    string[] part = l1.Split('|');
                    dt.Rows.Add(part[0], part[1], part[2], part[3], part[4]);

                    //dt.Rows.Add("Type: 18", "REC_DESCR", "ATT_TYPE", "ATT_VAL", "ATT_DESC", "#######");

                    foreach (string l2 in c)
                    {
                        string[] part2 = l2.Split('|');
                        if (part2[1] == part[1])
                        {
                            dt.Rows.Add("    ", part2[0], part2[1], part2[2], part2[3]);
                        }
                        
                    }
                    dt.Rows.Add("++++++++++", "++++++++++", "++++++++++", "++++++++++", "++++++++++");
                }
            }
            dataGridView1.DataSource = dt;
        }



        public void maake()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("1");
            dt.Columns.Add("2");
            dt.Columns.Add("3");
            dt.Columns.Add("4");
            dt.Columns.Add("5");
            dt.Columns.Add("6");

         //   dt.Rows.Add("REC_DESCR", "ATT_TYPE", "FIELD_SIZE", "DATA_TYPE", "ATT_DESC");

            using (StreamWriter sw = new StreamWriter(filename3))
            {
                foreach (string l1 in a)
                {
                    string[] part = l1.Split('|');
                    dt.Rows.Add(part[0], part[1], part[2], part[3], part[4]);

                    //dt.Rows.Add("Type: 18", "REC_DESCR", "ATT_TYPE", "ATT_VAL", "ATT_DESC", "#######");

                    foreach (string l2 in c)
                    {
                        string[] part2 = l2.Split('|');
                        if (part2[1] == part[1])
                        {
                            dt.Rows.Add(part2[0], part2[1], part2[2], part2[3]);
                        }

                    }
                  //  dt.Rows.Add("++++++++++", "++++++++++", "++++++++++", "++++++++++", "++++++++++");
                }


              //   dt.Rows.Add("07", "++++++++++", "++++++++++", "++++++++++", "++++++++++");
              //  dt.Rows.Add("REC_DESCR","FEAT_CODE","FEAT_NAME");

   
                    foreach (string l1 in b)
                    {
                        string[] part = l1.Split('|');
                        dt.Rows.Add(part[0], part[1], part[2]);
                        totaal++;
                    }
                






            }
            dataGridView1.DataSource = dt;
        }








        //Land Use and Land Cover Display Types
        public void land(string type)
        {
            DataTable dt = new DataTable();


            dt.Columns.Add("1");
            dt.Columns.Add("2");
            dt.Columns.Add("3");
            dt.Columns.Add("4");
            dt.Columns.Add("5");

            dt.Rows.Add("05", "#######", "#######, #######", "#######");
            dt.Rows.Add("REC_DESCR","ATT_TYPE","FIELD_SIZE","DATA_TYPE","ATT_DESC");

            using (StreamWriter sw = new StreamWriter(filename3))
            {
                foreach (string l1 in a)
                {
                    string[] part = l1.Split('|');
                    if (part[1] == type)
                    {
                        dt.Rows.Add(part[0], part[1], part[2], part[3], part[4]);
                    }
                }
            }

            dt.Rows.Add("18", "#######", "#######, #######", "#######");
            dt.Rows.Add("REC_DESCR","ATT_TYPE","ATT_VAL","ATT_DESC", "#######");

            using (StreamWriter sw = new StreamWriter(filename3))
            {
                foreach (string l1 in c)
                {

                    string[] part = l1.Split('|');
                    if (part[1] == type)
                    {
                        dt.Rows.Add(part[0], part[1], part[2], part[3], "#####");
                    }
                }
            }
            dataGridView1.DataSource = dt;
        }




        //Land Use and Land Cover Display Types
        public void showTypc(string type)
        {
            DataTable dt = new DataTable();


            dt.Columns.Add("1");
            dt.Columns.Add("2");
            dt.Columns.Add("3");
            dt.Columns.Add("4");
            dt.Columns.Add("5");

            dt.Rows.Add("18", "#######", "#######, #######", "#######");
            dt.Rows.Add("REC_DESCR", "ATT_TYPE", "ATT_VAL", "ATT_DESC", "#######");

                foreach (string l1 in c)
                {

                    string[] part = l1.Split('|');
                    if (part[1] == type)
                    {
                        dt.Rows.Add(part[0], part[1], part[2], part[3], "#####");
                    }
                }

        }










        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "05")
            {
                this.totaal = 0;
                label1.Text = "05 - Attribute Definition Record [ATDEFREC]";
                maaka();
            }
            if (comboBox1.Text == "07")
            {
                this.totaal = 0;
                label1.Text = "07 - Feature Definition Record [FEATDEFREC]";
                maakb();
            }
            if (comboBox1.Text == "18")
            {
                this.totaal = 0;
                label1.Text = "Attribute Value Definition Record [ATTVALREC]";
                maakc();
            }
            if (comboBox1.Text == "05-18")
            {
                this.totaal = 0;
                label1.Text = "koppeling tussen 15 en 18";
                maakd();
            }
            if (comboBox1.Text == "05-17-18")
            {
                this.totaal = 0;
                label1.Text = "koppeling tussen 15 en 18 + 18";
                maake();
            }
          





            label2.Text = totaal.ToString();
        }

        public void maakex()
        {
            Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
            xla.Visible = true;
            Excel.Workbook wb = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
            Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)xla.ActiveSheet;
            Excel.Worksheet ws2 = (Microsoft.Office.Interop.Excel.Worksheet)xla.ActiveSheet;

            int i = 2;

            ws.Cells[1, 1] = "REC_DESCR";
            ws.Cells[1, 2] = "FEAT_CODE";
            ws.Cells[1, 3] = "FEAT_NAME";

            foreach (string l1 in b)
            {
                string[] part = l1.Split(' ');
                ws.Cells[i, 1] = part[0];
                ws.Cells[i, 2] = part[1];
                ws.Cells[i, 3] = part[2];
                i++;
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*
            //Land Use and Land Cover Display Types 
            comboBox1.Items.Add("Beach/Dune/Sand Area"); // 7S

            comboBox1.Items.Add("Park/Garden"); //  PT
            comboBox1.Items.Add("Forest"); // 5F
            comboBox1.Items.Add("Company Ground"); //8G  
            comboBox2.Items.Add("Functional Road Class "); //FC  
             * */

            if (comboBox2.Text == "Forest")
            {
                label1.Text = "Forest";
                land("5F");
            }
            if (comboBox2.Text == "Beach/Dune/Sand Area")
            {
                label1.Text = "Beach/Dune/Sand Area";
                land("7S");
            }
            if (comboBox2.Text == "Park/Garden")
            {
                label1.Text = "Park/Garden";
                land("PT");
            }
            if (comboBox2.Text == "Company Ground")
            {
                label1.Text = "Company Ground";
                land("8G");
            }
            if (comboBox2.Text == "Functional Road Class")
            {
                label1.Text = "Functional Road Class";
                land("FC");
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
          
           land(textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            using (StreamWriter sw = new StreamWriter(zoom1))
            {
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {

                    sw.WriteLine(row.Cells[0].Value.ToString() + "|" + row.Cells[1].Value.ToString() + "|" + row.Cells[2].Value.ToString() + "|" + row.Cells[3].Value.ToString() + "|" + row.Cells[4].Value.ToString() + "|" + row.Cells[5].Value.ToString());
                }
            } 
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

            StreamReader sr = File.OpenText(zoom1);
            long totalbytes = 0;
            try
            {
                sr = File.OpenText(zoom1);
                totalbytes = sr.BaseStream.Length;
            }
            catch (Exception h)
            {
                Console.WriteLine("No GDF file found.");
                Environment.Exit(0);
            }

            DataTable dt = new DataTable();
            dt.Columns.Add("1");
            dt.Columns.Add("2");
            dt.Columns.Add("3");
            dt.Columns.Add("4");
            dt.Columns.Add("5");
            dt.Columns.Add("6");


            while (!sr.EndOfStream)
            {
                String str = sr.ReadLine();
                string[] part = str.Split('|');
             //   Console.WriteLine(str);
              //  dt.Rows.Add(str);
                dt.Rows.Add(part[0], part[1], part[2], part[3], part[4], part[5]);
               // Console.WriteLine(part[0], part[1], part[2], part[3], part[4]);

            }

            dataGridView1.DataSource = dt;
            /*
            DataTable dt = new DataTable();
            dt.Columns.Add("1");
            dt.Columns.Add("2");
            dt.Columns.Add("3");
            dt.Columns.Add("4");
            dt.Columns.Add("5");
            dt.Columns.Add("6");

             using (StreamWriter sw = new StreamWriter(zoom1))
            {

                    string[] part = l1.Split('|');


                    dt.Rows.Add(part[0], part[1], part[2], part[3], part[4], part[5]);

                }
             }

             dataGridView1.DataSource = dt;
             * */
        }

        private void button4_Click(object sender, EventArgs e)
        {
            StreamReader sr = File.OpenText(zoom1);
            long totalbytes = 0;
            try
            {
                sr = File.OpenText(zoom1);
                totalbytes = sr.BaseStream.Length;
            }
            catch (Exception h)
            {
                Console.WriteLine("No GDF file found.");
                Environment.Exit(0);
            }

            DataTable dt = new DataTable();
            dt.Columns.Add("1");
            dt.Columns.Add("2");
            dt.Columns.Add("3");
            dt.Columns.Add("4");
            dt.Columns.Add("5");
            dt.Columns.Add("6");


            while (!sr.EndOfStream)
            {
                String str = sr.ReadLine();
                string[] part = str.Split('|');
                if (part[5] != "")
                {
                    dt.Rows.Add(part[0], part[1], part[2], part[3], part[4], part[5]);
                }
 



            }

            dataGridView1.DataSource = dt;

        }



    }
}
